package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.AgoniFavoriteMapper;
import com.ruoyi.system.mapper.AgoniProductMapper;
import com.ruoyi.system.mapper.AgoniProductTypeMapper;
import com.ruoyi.system.pojo.entity.Favorite;
import com.ruoyi.system.service.IAgoniFavoriteService;
import com.ruoyi.system.service.IAgoniProductTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IAgoniFavoriteServiceImpl implements IAgoniFavoriteService {
    @Autowired
    private AgoniFavoriteMapper agoniFavoriteMapper;

    @Autowired
    private AgoniProductMapper agoniProductMapper;

    // TODO: 实现查询是否收藏

    @Override
    public Boolean isFavorite(Long userId, Long productId) {

        Favorite favorite = agoniFavoriteMapper.isFavorite(userId, productId);
        if (favorite == null) {
            return false;
        }
        return true;
    }

    // TODO: 实现收藏

    @Override
    public Boolean addFavorite(Long userId, Long productId) {
        // TODO: 查询商品类型
        Long productTypeId = agoniProductMapper.getCategoryById(productId);
        Favorite favorite = new Favorite();
        favorite.setUserId(Math.toIntExact(userId));
        favorite.setProductId(Math.toIntExact(productId));
        favorite.setCategoryId(Math.toIntExact(productTypeId));
        log.info("收藏商品", favorite);
        agoniFavoriteMapper.addFavorite(favorite);
        return true;
    }
}
